﻿using Coldairarrow.Business.Base_Manage;
using Coldairarrow.Entity.Base_leasesystem_employeesign;
using Coldairarrow.Entity.Base_leasesystem_signfile;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base_leasesystem_signfile
{
    public class leasesystem_signfileBusiness : BaseBusiness<leasesystem_signfile>, Ileasesystem_signfileBusiness, ITransientDependency
    {
        public leasesystem_signfileBusiness(IDbAccessor db, IHomeBusiness homeBusiness)
            : base(db)
        {
            _homebusiness = homeBusiness;
        }
        IHomeBusiness _homebusiness { get; set; }
        #region 外部接口

        public async Task<PageResult<leasesystem_signfile>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<leasesystem_signfile>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<leasesystem_signfile, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<leasesystem_signfile> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(leasesystem_signfile data)
        {
            await InsertAsync(data);
        }

        public async Task AddDataEmpSignAsync(leasesystem_signfile data)
        {
            await InsertAsync(data);
            var empsign = Db.GetIQueryable<leasesystem_employeesign>().Where(a => a.SignFiled == data.Rid).FirstOrDefault();
            empsign.zt = 1;
             Db.Update(empsign);
        }


        public async Task UpdateDataAsync(leasesystem_signfile data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            for (int i = 0; i < ids.Count(); i++)
            {
                var entity = GetEntity(ids[i]);
                if (!entity.ContractdownUrl.IsNullOrEmpty())
                {
                    string ContractdownUrl = entity.ContractdownUrl.Split('/')[4];
                    await  _homebusiness.DeleteFile(Directory.GetCurrentDirectory() + $"\\wwwroot\\Upload\\{ContractdownUrl}");
                    
                }
               await DeleteAsync(ids[i]);
            }
        }

        #endregion

        #region 私有成员

        #endregion
    }
}